package de.havre.wakeupnas.ui;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.TrayIcon.MessageType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import de.havre.wakeupnas.nas.NasController;
import de.havre.wakeupnas.nas.NasState;
import de.havre.wakeupnas.nas.NasStateListener;

public class TrayTool implements NasStateListener {

	private Image onIMage;

	private Image offImage;

	private Image readyImage;

	private Image syncImage;

	private TrayIcon trayIcon;

	private MenuItem onOffItem;

	private MenuItem syncItem;

	private MenuItem exitItem;
	
	private MenuItem folderItem;

	private PopupMenu popup;

	public TrayTool(final NasController controller) {

		onIMage = Toolkit.getDefaultToolkit().getImage(
				Class.class.getResource("/iconOn.jpg"));
		offImage = Toolkit.getDefaultToolkit().getImage(
				Class.class.getResource("/iconOff.jpg"));
		readyImage = Toolkit.getDefaultToolkit().getImage(
				Class.class.getResource("/iconReady.jpg"));
		syncImage = Toolkit.getDefaultToolkit().getImage(
				Class.class.getResource("/iconSync.jpg"));

		if (SystemTray.isSupported()) {

			// create a popup menu
			popup = new PopupMenu();

			ActionListener onOffListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (controller.getState().equals(NasState.OFF)) {
						controller.switchOn();
					} else {
						controller.switchOff();
					}
				}
			};
			onOffItem = new MenuItem("switch on");
			onOffItem.addActionListener(onOffListener);
			popup.add(onOffItem);

			ActionListener exitListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					System.exit(0);
				}
			};
			exitItem = new MenuItem("exit");
			exitItem.addActionListener(exitListener);
			popup.add(exitItem);

			ActionListener syncListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					controller.startSync();
				}
			};
			syncItem = new MenuItem("start sync");
			syncItem.addActionListener(syncListener);
			popup.add(syncItem);
			
			ActionListener folderListener = new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					controller.showFolder();
				}
			};
			folderItem = new MenuItem("show folder");
			folderItem.addActionListener(folderListener);
			popup.add(folderItem);

			try {
				SystemTray tray = SystemTray.getSystemTray();
				trayIcon = new TrayIcon(onIMage, "WakeOnNas", popup);
				tray.add(trayIcon);
			} catch (AWTException e) {
				System.err.println(e);
			}
		} else {
			System.err.println("Tray Icon is not supported from OS");
		}

	}

	@Override
	public void notifyNasState(NasState nasState) {
		if (nasState.equals(NasState.OFF)) {
			trayIcon.setImage(offImage);
			onOffItem.setLabel("switch on");
			syncItem.setEnabled(true);
			folderItem.setEnabled(false);
		} else if (nasState.equals(NasState.ON)) {
			trayIcon.setImage(onIMage);
			onOffItem.setLabel("switch off");
			syncItem.setEnabled(true);
			folderItem.setEnabled(false);
		} else if (nasState.equals(NasState.READY)) {
			trayIcon.setImage(readyImage);
			onOffItem.setLabel("switch off");
			syncItem.setEnabled(true);
			folderItem.setEnabled(true);
		} else if (nasState.equals(NasState.SYNC)) {
			trayIcon.setImage(syncImage);
			syncItem.setEnabled(false);
			folderItem.setEnabled(true);
		}

	}

	@Override
	public void notifyInfo(String info) {
		trayIcon.displayMessage("WakeOnNas Info", info, MessageType.INFO);
	}

}
